#include "shop_scene.h"
#include <QPixmap>



Shop_Scene::Shop_Scene(Data_Manager * data_manager,QWidget *parent)
    :Scene_qt(parent),
    data_manager(data_manager),
    goldNum(data_manager->gold_number)
{

    this->setWindowTitle("shop 全局强化界面");
    setupBtn();
    QPixmap pix;
    pix.load(":/resource/img/map.png");
    moneyBg = scene->addPixmap(pix);

    // 画钱袋子
    MyPushButton *moneyBtn = new MyPushButton(":/resource/img/shop/money.png",":/resource/img/shop/money.png",100,100);
    moneyBtn->setParent(this);
    moneyBtn->setFixedSize(100,100);
    moneyBtn->move(0,GAME_HEIGHT - 88);
    moneylabel->setParent(this);
    moneylabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    moneylabel->setText(QString("金币数量:%1").arg(goldNum));
    moneylabel->setGeometry(88,GAME_HEIGHT - 88,100,100);
    moneylabel->show();
    Btns.append(moneyBtn);
    update();



}
void Shop_Scene::setupBtn(){
    int btW = 150, btH = 100;

    QPushButton *btn1 = new QPushButton("100金币购买\n解锁交付中心扩大",this);
    btn1->setParent(this);
    btn1->setFixedSize(150,100);
    btn1->move(160+0*((GAME_WIDTH-320)/3)+100+20,110+0*((GAME_HEIGHT-110)/2));
    btn1->show();
    connect(btn1,&QPushButton::clicked,this,[=]()
    {
            if(goldNum >= 100)
                {

                    if(data_manager->getHubProgress() == 1) btn1->setText("已达到最大解锁进度");
                    if(data_manager->getHubProgress() < 1)
                    {
                        goldNum -= 100;
                        ++data_manager->hub_progress;
                    }
                }
            update();
    });

    QPushButton *btn2 = new QPushButton("500金币购买\n解锁地图扩大",this);
    btn2->setParent(this);
    btn2->setFixedSize(150,100);
    btn2->move(160+1*((GAME_WIDTH-320)/3)+100+20,110+0*((GAME_HEIGHT-110)/2));
    btn2->show();
    connect(btn2,&QPushButton::clicked,this,[=]()
            {
            if(goldNum >= 500)
                {

                    if(data_manager->getGridProgress() == 2) btn2->setText("已达到最大解锁进度");
                    if(data_manager->getGridProgress() < 2)
                    {
                        goldNum -= 500;
                        ++data_manager->grid_progress;
                    }
                }
    update();
    });

    QPushButton *btn3 = new QPushButton("800金币购买\n解锁利润增长",this);
    btn3->setParent(this);
    btn3->setFixedSize(150,100);
    btn3->move(160+2*((GAME_WIDTH-320)/3)+100+20,110+0*((GAME_HEIGHT-110)/2));
    btn3->show();
    connect(btn3,&QPushButton::clicked,this,[=]()
    {
        if(goldNum >= 800)
        {

            if(data_manager->interest_rate_progress == 3) btn3->setText("已达到最大解锁进度");
            if(data_manager->interest_rate_progress < 3)
            {
                goldNum -= 800;
                ++data_manager->interest_rate_progress;
            }
        }
    update();
    });

    QPushButton *btn4 = new QPushButton("1000金币购买\n解锁矿物变多TBC",this);
    btn4->setParent(this);
    btn4->setFixedSize(150,100);
    btn4->move(160+0*((GAME_WIDTH-320)/3)+100+20,60+1*((GAME_HEIGHT-110)/2));
    btn4->show();
    connect(btn4,&QPushButton::clicked,this,[=]()
            {
                // if(goldNum>= 1000)
                // {
                //     goldNum -= 1000;
                //     if( == ) btn4->setText("已达到最大解锁进度");
                //     if( < )
                //     {
                //     }
                // }
    update();
    });

    QPushButton *btn5 = new QPushButton("2000金币购买\n解锁TBC",this);
    btn5->setParent(this);
    btn5->setFixedSize(150,100);
    btn5->move(160+1*((GAME_WIDTH-320)/3)+100+20,60+1*((GAME_HEIGHT-110)/2));
    btn5->show();
    connect(btn5,&QPushButton::clicked,this,[=](){});

    QPushButton *btn6 = new QPushButton("3000金币购买\n解锁TBC",this);
    btn6->setParent(this);
    btn6->setFixedSize(150,100);
    btn6->move(160+2*((GAME_WIDTH-320)/3)+100+20,60+1*((GAME_HEIGHT-110)/2));
    btn6->show();
    connect(btn6,&QPushButton::clicked,this,[=](){});

    Btns = {btn1,btn2,btn3,btn4,btn5,btn6};
}

void Shop_Scene::setupLabel(){
    // painter1.drawPixmap(160+0*((GAME_WIDTH-320)/3),110+0*((GAME_HEIGHT-110)/2),100,100,pix1);
    label1->setParent(this);
    label1->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    label1->setText(QString("交付中心扩大\n至多1次\n\n已解锁进度:%1").arg(data_manager->hub_progress));
    label1->setGeometry(160+0*((GAME_WIDTH-320)/3),110+0*((GAME_HEIGHT-110)/2)+100,100,100);
    label1->show();


    // painter2.drawPixmap(160+1*((GAME_WIDTH-320)/3),110+0*((GAME_HEIGHT-110)/2),100,100,pix2);
    label2->setParent(this);
    label2->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    label2->setText(QString("地图范围扩大\n至多2次\n\n已解锁进度:%1").arg(data_manager->grid_progress));
    label2->setGeometry(160+1*((GAME_WIDTH-320)/3),110+0*((GAME_HEIGHT-110)/2)+100,100,100);
    label2->show();


    // painter3.drawPixmap(160+2*((GAME_WIDTH-320)/3),110+0*((GAME_HEIGHT-110)/2),100,100,pix3);
    label3->setParent(this);
    label3->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    label3->setText(QString("单次获利变多\n至多加3次\n\n已解锁进度:%1").arg(data_manager->interest_rate_progress));
    label3->setGeometry(160+2*((GAME_WIDTH-320)/3),110+0*((GAME_HEIGHT-110)/2)+100,100,100);
    label3->show();


    // painter4.drawPixmap(160+0*((GAME_WIDTH-320)/3),60+1*((GAME_HEIGHT-110)/2),100,100,pix4);
    label4->setParent(this);
    label4->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    label4->setText(QString("地图矿物更多\n至多3次\n\n暂不可购买:%1").arg(0));
    label4->setGeometry(160+0*((GAME_WIDTH-320)/3),60+1*((GAME_HEIGHT-110)/2)+100,100,100);
    label4->show();


    // painter5.drawPixmap(160+1*((GAME_WIDTH-320)/3),60+1*((GAME_HEIGHT-110)/2),100,100,pix5);
    label5->setParent(this);
    label5->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    label5->setText(QString("暂不可购买:%1").arg(0));
    label5->setGeometry(160+1*((GAME_WIDTH-320)/3),60+1*((GAME_HEIGHT-110)/2)+100,100,100);
    label5->show();


    // painter6.drawPixmap(160+2*((GAME_WIDTH-320)/3),60+1*((GAME_HEIGHT-110)/2),100,100,pix6);
    label6->setParent(this);
    label6->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    label6->setText(QString("暂不可购买:%1").arg(0));
    label6->setGeometry(160+2*((GAME_WIDTH-320)/3),60+1*((GAME_HEIGHT-110)/2)+100,100,100);
    label6->show();
}

void Shop_Scene::paintEvent(QPaintEvent *){

    moneylabel->setText(QString("金币数量:%1").arg(goldNum));
    moneylabel->setGeometry(88,GAME_HEIGHT - 88,100,100);
    setupLabel();
}
Shop_Scene::~Shop_Scene(){
    for(auto &b:Btns){
        if(b){
            delete b;
            b = null;
        }
    }
    Btns.clear();
    if(moneyBg){scene->removeItem(moneyBg); delete moneyBg; moneyBg = nullptr;}
}
